package com.zy.scwproject.controller;

import com.zy.scwcommon.bean.AppResponse;
import com.zy.scwcommon.entity.TMember;
import com.zy.scwproject.entity.*;
import com.zy.scwproject.service.ProjectInfoService;
import com.zy.scwproject.service.TMemberServiceFeigns;
import com.zy.scwproject.service.TMemberServiceFeigns;
import com.zy.scwproject.vo.req.ProjectDetailVo;
import com.zy.scwproject.vo.req.ProjectVo;
import com.zy.scwproject.vo.req.ReturnPayConfirmVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.Member;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:项目的查询接口
 * @Param:
 * @return:
 * @Author: zy
 * @Date:
 */
@Api(tags = "项目信息模块")
@RequestMapping("/project")
@RestController
@Slf4j
public class ProjectInfoController {
	@Autowired
	ProjectInfoService projectInfoService;
    @Autowired
	TMemberServiceFeigns tMemberServiceFeigns;
//	@ApiOperation(value="1.获取首页广告项目")
//	@GetMapping("/adv")
//	public AppResponse<ProjectVo> adv( ){
//
//		return AppResponse.ok(null);
//	}
	/**
	 * @Description:banner 下方展示前三个
	 * @Param:
	 * @return:
	 * @Author: zy
	 * @Date:
	 */
	@ApiOperation(value="2.===获取项目总览列表") //注意，远程调用的是get post 方式是哪个
	@PostMapping("/all")
	public AppResponse<Object> all(){
		// 1、分步查询，先查出所有项目
		// 2、再查询这些项目图片
		log.debug("调用总览列表成功======");
		List<ProjectVo> prosVo = new ArrayList<>();

		// 1、连接查询，所有的项目left join 图片表，查出所有的图片
		// left join：笛卡尔积 A*B 1000万*6 = 6000万
		// 大表禁止连接查询；
		List<TProject> pros = projectInfoService.getAllProjects();

		for (TProject tProject : pros) {
			Integer id = tProject.getId();
			List<TProjectImages> images = projectInfoService.getProjectImages(id);
			ProjectVo projectVo = new ProjectVo();
			BeanUtils.copyProperties(tProject, projectVo);
			for (TProjectImages tProjectImages : images) {
				if (tProjectImages.getImgtype() == 0) {
					projectVo.setHeaderImage(tProjectImages.getImgurl());
				} else {
					List<String> detailsImage =    projectVo.getDetailsImage();
					detailsImage.add(tProjectImages.getImgurl());
				}
			}
			prosVo.add(projectVo);

		}

		return AppResponse.ok(prosVo);
	}
	@ApiOperation(value="3.获取项目详情信息")
	@GetMapping("/details/info/{projectId}")
	public AppResponse<ProjectDetailVo> detailsInfo(@PathVariable("projectId") Integer projectId) {
		ProjectDetailVo detailVo = new ProjectDetailVo();
		TProject projectInfo = projectInfoService.getProjectInfo(projectId);
		//1.查出这个项目的所有图片
		List<TProjectImages> images = projectInfoService.getProjectImages(projectId);
		for (TProjectImages image : images) {
			if (image.getImgtype()==0){//头图
				detailVo.setHeaderImage(image.getImgurl());
			}else {
 				List<String> detailsImage = detailVo.getDetailsImage();
				detailsImage.add(image.getImgurl());
				//detailVo.setDetailsImage(detailsImage);
			}
		}
		//2.项目所有支持的档位
		List<TReturn> projectReturns = projectInfoService.getProjectReturns(projectId);
	    detailVo.setProjectReturns(projectReturns);
		BeanUtils.copyProperties(projectInfo,detailVo);

		return AppResponse.ok(detailVo);
	}
	@ApiOperation(value="4.获取项目支持的接口--确认回报信息")
	@GetMapping("/sys/support/{projectId}/{returnId}")
	public AppResponse<ReturnPayConfirmVo> support(@PathVariable("projectId") Integer projectId,
												   @PathVariable("returnId") Integer returnId){
		ReturnPayConfirmVo vo = new ReturnPayConfirmVo();

		//部分1：回报数据
		TReturn tReturn = projectInfoService.getProjectReturnById(returnId);
		vo.setReturnId(tReturn.getId());
		vo.setReturnContent(tReturn.getContent());
		vo.setNum(1);
		vo.setPrice(tReturn.getSupportmoney());
		vo.setFreight(tReturn.getFreight());
		vo.setSignalpurchase(tReturn.getSignalpurchase());
		vo.setPurchase(tReturn.getPurchase());


		//部分2：项目数据
		TProject project = projectInfoService.getProjectInfo(projectId);
		vo.setProjectId(project.getId());
		vo.setProjectName(project.getName());
		vo.setProjectRemark(project.getRemark());

		//部分3：发起人数据
		Integer memberid = project.getMemberid();
		AppResponse<TMember> resp = tMemberServiceFeigns.selectById(memberid);  //调用远程获取会员数据。
		TMember member = resp.getData();

		vo.setMemberId(member.getId());
		vo.setMemberName(member.getLoginacct());


		vo.setTotalPrice(new BigDecimal(vo.getNum() * vo.getPrice() + vo.getFreight()));

		return AppResponse.ok(vo);
	}
/**
 * @Description:通过回报id查询回报数据
 * @Param:
 * @return:
 * @Author: zy
 * @Date:
 */
	@GetMapping("/details/returns/info/{returnId}")
	public AppResponse<TReturn> returnInfo(@PathVariable("returnId") Integer returnId){
		TReturn tReturn = projectInfoService.getProjectReturnById(returnId);
		return AppResponse.ok(tReturn);
	}
	@ApiOperation(value=".获取项目系统分类信息")
	@GetMapping("/sys/type")
	public AppResponse<List<TType>> systype(){
		List<TType> projectTypes = projectInfoService.getProjectTypes();
		return AppResponse.ok(projectTypes);
	}


	@ApiOperation(value=".获取项目系统标签信息")
	@GetMapping("/sys/tags")
	public AppResponse<List<TTag>> systags(){
			List<TTag> projectTags = projectInfoService.getAllProjectTags();
			return AppResponse.ok(projectTags);
	}

	@ApiOperation(value=".获取项目回报档位信息")
	@GetMapping("/return/info")
	public AppResponse<List<TReturn>> support(Integer projectId){
			List<TReturn> projectReturns = projectInfoService.getProjectReturns(projectId);
			return AppResponse.ok(projectReturns);
	}

	@ApiOperation(value=".获取首页热门推荐项目")
	@GetMapping("/recommend/index")
	public AppResponse<List<ProjectVo>> index(){
		log.debug("调用首页前三个展示成功======");
		List<ProjectVo> prosVo = new ArrayList<>();

		// 1、连接查询，所有的项目left join 图片表，查出所有的图片
		// left join：笛卡尔积 A*B 1000万*6 = 6000万
		// 大表禁止连接查询；
		List<TProject> pros = projectInfoService.getAllProjects();
		for (TProject tProject : pros) {
			Integer id = tProject.getId();
			List<TProjectImages> images = projectInfoService.getProjectImages(id);
			ProjectVo projectVo = new ProjectVo();
			BeanUtils.copyProperties(tProject, projectVo);

			for (TProjectImages tProjectImages : images) {
				if (tProjectImages.getImgtype() == 0) {
					projectVo.setHeaderImage(tProjectImages.getImgurl());
				} else {
					List<String> detailsImage = projectVo.getDetailsImage();
					detailsImage.add(tProjectImages.getImgurl());
				}
			}
			prosVo.add(projectVo);

		}

		return AppResponse.ok(prosVo);
		}
/**
 * @Description:分类项目展示的前四个
 * @Param:
 * @return:
 * @Author: zy
 * @Date:
 */
	@ApiOperation(value="获取首页分类推荐项目")
	@GetMapping("/recommend/type")
	public AppResponse<List<TType>> type(){
		List<TType>  types=new ArrayList<TType>();
			List<TType> projectTypes = projectInfoService.getProjectTypes();
			for (TType projectType : projectTypes) {
				if (projectTypes.size()<4){
				types.add(projectType);
				return AppResponse.ok(types);
				}
			}
			return AppResponse.fail(null);
	}

	
}
